---------------------------------------------------------------------------------------------------------------------------
      name:  <unnamed>
       log:  C:\Users\user\Documents\work/DNB_survey_consumption/ConGrowth/Replication/Programs/Bounds/PI_dist_sptri_b1000.
> log
  log type:  text
 opened on:  14 Dec 2018, 12:38:58

.    
. global stdate $S_DATE 

. global sttime $S_TIME   

. noisily display "Begin program at ${stdate} ${sttime}"   /* time stamp */   
Begin program at 14 Dec 2018 12:38:58

.    
. *Loop over outcomes      
. forvalues out=1/$outno {      
  2.    global out `out'      
  3.    local out`out': word `out' of ${outl}      
  4.       
.    global out`out' `out`out''      
  5.    global outn: word `out' of ${outl}      
  6.       
.    *Loop over treatments      
.    *First element of the loop      
.    local fltv: word 1 of ${gtlist}       
  7.       
.    forvalues tv=1/$tno {      
  8.       global tv `tv'
  9.       global treat: word `tv' of ${gtlist}      
 10.       
.       *Reading the data                  
.       qui use "${projf}/Data/Data_cons_uncertainty.dta", clear       
 11. 
.       *There are few observations in the 2nd quantile, and thus we merge it with the
.       *first quantile
.       if ("${minstr1}"=="q10_hvsptri_incgr" | "${minstr1}"=="q10_hvtrio_incgr")  {      
 12.          qui replace ${minstr1}=1 if ${minstr1}==2      
 13.       }      
 14.       if ("${minstr1}"=="q10_hvsptri_incgr" | "${minstr1}"=="q10_hvtrio_incgr") {      
 15.          qui replace ${minstr1}=${minstr1}-1 if ${minstr1}>1 & ${minstr1}<.      
 16.       }      
 17. 
.       *Dropping observations in which the outcome, treatment or the sampling weight is missing            
.       qui drop if (${out${out}}>=.) | ${treat}>=. | (${wgt}>=. | ${wgt}<${tolr})      
 18.          
.       *Defining the treatments
.       qui do "${projf}/Programs/Bounds/Conc_Unc_treat_definitions.do"  
 19.          
.       *Deleting missing values so as to keep sample size the same across distributions      
.       qui keep if regio<.       
 20.       if "${dist}"=="trio" {      
 21.             
.          qui keep if hmsptri_sqcgr<. & hvsptri_incgr<.               
 22.       }      
 23.             
.       *Maximum and minimum treatment values      
.       qui su ${treat} [aw=${wgt}], d      
 24.       global max${treat} = r(max)      
 25.       global min${treat} = r(min)      
 26. 
.       *Maximum and minimum possible values of outcomes
.       qui su ${out`out'} [aw=${wgt}]       
 27.       global min${outn} = r(min)      
 28.       global max${outn} = r(max)      
 29.       
.       *Number of observations      
.       qui count 
 30.       sca nobs_nomiv = r(N)     
 31.             
.       *Number of obs for both samples, taking into account the MIV assumption instruments                  
.       if "${minstr1}"~="" {                  
 32.          
.          qui count if ${minstr1}~=.       
 33.          sca nobs_miv1 = r(N)            
 34.       }                  
 35. 
.       keep nohhold ${out${out}} ${treat} ${minstr1} ${wgt} ${clustv}             
 36.             
.       qui save "${projf}/Data/temp1.dta", replace            
 37.          
.       *Global of column names of the output matrix            
.       global cl lb ub 
 38.       forvalues npv=1/$ncicv {      
 39.          local pv: word `npv' of ${cicv}      
 40.          local pv1=100*`pv'      
 41.          global cl ${cl} `pv1'_bci_low `pv1'_bci_high `pv1'_imci_low `pv1'_imci_high             
 42.       }      
 43. 
.       global cln: word count ${cl}            
 44.          
.       global cl ${cl} blank      
 45.       
.              
.       *List of output from the bootstrapped program      
. 
.       *Global of level coefficients
.       global levcoeff ""
 46. 
.       *We then put in the outcomes      
.       *Loop over methods (excluding the ones without the bias adjustment)      
. 
.       foreach mth in $gmethod {      
 47. 
.          *Loop over treatments            
.          forvalues t=1/$vtn {            
 48. 
.             *Loop over statistics      
.             foreach st in $stat {      
 49.                
.                *We then add the outcomes            
.                global reslist ${reslist} `st'_tr`t'_lb_`mth' `st'_tr`t'_ub_`mth'                             
 50.                global levcoeff ${levcoeff} `st'_tr`t'_lb_`mth' `st'_tr`t'_ub_`mth'                             
 51.                            
.                *Adding the differences across values of the treatment            
.                local jj = `t'+ 1             
 52.                forvalues m = `jj'/$vtn {            
 53.                
.                   global reslist ${reslist} `st'_tr`m'_tr`t'_lb_`mth' `st'_tr`m'_tr`t'_ub_`mth'            
 54.                }            
 55.              
.             *End of loop over statistics below      
.             }        
 56.          *End of loop over treatments below            
.          }            
 57.       *End of loop over methods below      
.       }      
 58. 
.       *Size of results list         
.       global blistn: word count ${reslist}         
 59.             
.       *Generating a matrix into which the bootstrap results will be put         
.       mat BR = J(${bsn},${blistn},.)         
 60.       global cbr ""         
 61.       foreach rs in $reslist {         
 62. 
.          global cbr ${cbr} v`rs'         
 63.       }         
 64.       mat colnames BR = ${cbr}         
 65.             
.       *Showing the combination for which calculations are performed         
.       di in yellow "Outcome ${out`out'}, treat ${treat}, miv1 ${minstr1}"         
 66. 
.       local sd = ${tv} + ${out}            
 67.       set seed `sd'         
 68.       *Indicator of boostrap runs         
.       global sr = 0         
 69. 
.       *Running the calculations program                        
.       global testw [aw=${wgt}]                  
 70.       qui do "${projf}/Programs/Bounds/Conc_Unc_calculations.do"                         
 71.       
.       *Recording the point estimates 
.       foreach rs in $reslist {
 72.          sca b_`rs' = `rs'
 73.          sca testb_`rs' = b_`rs'
 74.          sca drop `rs'
 75.       }
 76. 
.       *Bootstrap program
.       di in yellow "Bootstrap replicates"
 77.       forvalues bb=1/$bsn {
 78. 
.          *Defining an index that will determine whether the loop finishes or not
.          if `bb'==1 {
 79.             local sr = 1
 80.          }
 81.          else if `bb'>1 {
 82.          
.          *Augmenting the index that tracks valid simulation runs by one
.            local sr = `sr' + 1
 83.          }
 84.          global sr = `bb'
 85.          *di in red "`sr'"
. 
.          local sd = ${tv} + ${out} + `bb'    
 86.          set seed `sd'
 87.          
.          global testw [aw=${wgt}]              
 88.          qui use "${projf}/Data/temp1.dta", clear
 89.          bsample,cluster(${clustv}) 
 90.          
.          *Running the calculations program
.          qui do "${projf}/Programs/Bounds/Conc_Unc_calculations.do"             
 91. 
.          *Putting the results in the relevant variables      
.          forvalues mc=1/$blistn {      
 92.             
.             local brv: word `mc' of ${reslist}      
 93.             mat BR[`sr',`mc']=`brv'      
 94.             sca drop `brv'
 95.          }      
 96.          *Periodically displaying the no of runs
.          forvalues kk=1/5 {
 97.             
.             if `sr'==$bsn*(`kk'/5) {
 98.                
.                di in yellow "Bootstrap run no. `sr'"
 99.             }
100.          }
101. 
.       *End of bootstrap loop below      
.       }      
102. 
.       drop _all
103.                
.       *Saving the matrix of results as new variables
.       qui svmat double BR, names(col)
104. 
.       *Saving the dataset
.       qui save "${projf}/Data/tempres.dta", replace
105.       qui save "${projf}/Data/tempres1.dta", replace
106.                
.       assert _N==${bsn}         
107.            
.       *Calculating magnitudes of interest
.       forvalues an=1/$blistn {
108.                   
.          local rs: word `an' of ${reslist}
109.                   
.          *Checking that the results have been saved properly                  
.          assert v`rs'<.
110. 
.          *Average of the point estimates of the boostrap run                  
.          qui su v`rs'
111.          sca av_`rs' = r(mean)
112.          sca se_`rs' = r(sd)
113.          assert se_`rs'<.
114. 
.          *Bootstrap bias 
.          foreach cs in $levcoeff {
115.             if "`rs'"=="`cs'" {
116.          
.                sca bias_`rs'= av_`rs' - b_`rs'         
117.                assert bias_`rs'<.                        
118.             }
119.          }
120.       } 
121. 
.       *For methods not involving instruments, we put the bias equal to 0         
.       foreach mth in $gnobias0 {         
122. 
.          *Loop over statistics         
.           foreach st in $stat { 
123. 
.             forvalues t=1/$vtn {         
124.   
.                foreach bb in lb ub {         
125. 
.                   sca bias_`st'_tr`t'_`bb'_`mth' = 0         
126.                }                              
127.             *End of loop over treatment values below         
.             }         
128.          *End of loop over statistcs below         
.          }         
129.       *End of loop over methods below         
.       }          
130. 
.       *After calculating the bias, we substract it from the estimates of the bounds in each            
.       *bootstrap run. Then, we calculate an indicator                  
.       *that shows the percentage of runs for which the bias-corrected lower bound is higher than                       
.       *the corresponding upper one                     
.       foreach mth in $gnobias0 $gbias0 {                       
131. 
.          *Loop over statistics                 
.          foreach st in $stat {                 
132. 
.             *Loop over values of the treatment                 
.             forvalues t=1/$vtn {               
133.                                
.                foreach bb in lb ub {           
134. 
.                   *Recording the values of the bootstrap run before the bias correction         
.                   foreach mth2 in $gbias0 {         
135.                                  
.                      if "`mth'"=="`mth2'" {         
136. 
.                         qui gen double v`st'_tr`t'_`bb'_`mth2'nb = v`st'_tr`t'_`bb'_`mth2'                       
137.                       } 
138.                   }             
139. 
.                   qui replace v`st'_tr`t'_`bb'_`mth' = max(${min${outn}}, ///          
>                      min(v`st'_tr`t'_`bb'_`mth' - bias_`st'_tr`t'_`bb'_`mth',${max${outn}}))                            
140.                                
.                   *Checking that the bias corrected values fall between the overall minima and maxima          
.                   assert v`st'_tr`t'_`bb'_`mth'>${min${outn}}-${tolr} & ///                     
>                      v`st'_tr`t'_`bb'_`mth'<${max${outn}}+${tolr}                       
141.                }                                                
142.                                
.                *Indicator for the % of bootstrap runs in which LB>UB          
.                qui gen byte pr_`t'_`mth' = (v`st'_tr`t'_lb_`mth'>v`st'_tr`t'_ub_`mth'+${tolr})                          
143.                qui su pr_`t'_`mth', meanonly                            
144.                  
.                foreach s1 in $gnobias0 {                                                
145.                                
.                   if "`s1'"=="`mth'" {          
146.                                 
.                      global pcr_`st'_tr`t'_`mth' = 100*round(r(mean),1e-2)                      
147.                   }                    
148.                }                       
149.                foreach s1 in $gbias0 {                                                  
150.                                
.                   if "`s1'"=="`mth'" {          
151.                                 
.                      global pcr_`st'_tr`t'_`mth' = 100*round(r(mean),1e-2)              
152.                      global pcr_`st'_tr`t'_`mth'nb = ${pcr_`st'_tr`t'_`mth'}                    
153.                   }                    
154.               }                        
155.                                
.               drop pr_`t'_`mth'          
156. 
.             *End of loop over treatment values below           
.             }                          
157.          *End of loop over statistcs below                     
.          }                     
158.       *End of loop over methods below                  
.       }                        
159. 
.       qui save "${projf}/Data/tempres.dta", replace            
160. 
.       *For methods that use monotone instruments 
.       *we need to substract the bias in order to get the point estimates                       
.       *We also record the estimates and std errors without the bias correction                         
. 
.       *Loop over methods using instruments             
.       foreach mth in $gbias0 {                         
161.          global mth `mth'              
162. 
.          *Loop over statistics                 
.          foreach st in $stat {                 
163.                        
.             global st `st'             
164.                        
.             *Loop over treatments                              
.             forvalues t=1/$vtn {                               
165.                                
.                global t = `t'                  
166.                                
.                *First, we record the values and std. errors before any corrections             
.                *Loop over bounds                       
.                foreach bb in lb ub {                           
167.                                
.                   sca b_`st'_tr`t'_`bb'_`mth'nb = b_`st'_tr`t'_`bb'_`mth'                              
168.                   sca se_`st'_tr`t'_`bb'_`mth'nb = se_`st'_tr`t'_`bb'_`mth'            
169.                   sca bias_`st'_tr`t'_`bb'_`mth'nb = bias_`st'_tr`t'_`bb'_`mth'                
170.                                
.                   *Adding the differences across values of the treatment                       
.                   local jj = `t'+ 1                            
171.                   forvalues m = `jj'/$vtn {                            
172.                                        
.                      sca b_`st'_tr`m'_tr`t'_`bb'_`mth'nb = b_`st'_tr`m'_tr`t'_`bb'_`mth'                               
173.                      sca se_`st'_tr`m'_tr`t'_`bb'_`mth'nb = se_`st'_tr`m'_tr`t'_`bb'_`mth'                     
174.                      *sca drop se_`st'_tr`m'_tr`t'_`bb'_`mth'                  
.                   }                    
175.                }               
176.  
.                *Indicator for the % of bootstrap runs in which LB>UB           
.                qui gen byte pr_`t'_`mth' = (v`st'_tr`t'_lb_`mth'>v`st'_tr`t'_ub_`mth'+${tolr})                          
177.                qui su pr_`t'_`mth', meanonly                            
178.                global pcr_`st'_tr`t'_`mth'=100*round(r(mean),1e-2)                      
179.                drop pr_`t'_`mth'          
180. 
.                *If there is no problem with the boostrap runs or with the point estimates of the LB and UB             
.                *then we substract the bias from the point estimates                    
.                if (b_`st'_tr`t'_lb_`mth' - bias_`st'_tr`t'_lb_`mth' <= ///                     
>                   b_`st'_tr`t'_ub_`mth' - bias_`st'_tr`t'_ub_`mth'+${tolr}) {                          
181.                                        
.                   *Loop over bounds                            
.                   foreach bb in lb ub {                                
182.                                        
.                      if "`bb'"=="lb" {                                 
183.                                
.                         sca b_`st'_tr`t'_`bb'_`mth' = max(${min${outn}}, min(b_`st'_tr`t'_`bb'_`mth' - ///             
>                            bias_`st'_tr`t'_`bb'_`mth',${max${outn}}))                          
184.                      }                                 
185.                      if "`bb'"=="ub" {                                 
186.                                
.                         sca b_`st'_tr`t'_`bb'_`mth' = min(${max${outn}}, max(b_`st'_tr`t'_`bb'_`mth' - ///             
>                            bias_`st'_tr`t'_`bb'_`mth',${min${outn}}))                          
187.                      }                                 
188.                                
.                      *Checking that the values after the bias correction fall in the range defined by the                
>       
.                      *overall mininum and maximum                              
.                      assert b_`st'_tr`t'_`bb'_`mth'>${min${outn}}-${tolr} &  b_`st'_tr`t'_`bb'_`mth'<${max${outn}}+${tolr
> }                             
189.                                        
.                   *End of loop over bounds below                               
.                   }                                                    
190.                *End of if conditions over the percentage of problematic boostrap runs                          
.                }                       
191. 
.                if (b_`st'_tr`t'_ub_`mth'+${tolr}<b_`st'_tr`t'_lb_`mth') {                      
192.                   di "Upper bound smaller than lower bound, statistic `st', treat, `t', method `mth'"                 
>          
193.                   di b_`st'_tr`t'_ub_`mth' b_`st'_tr`t'_lb_`mth'                       
194.                   stop                         
195.                }                       
196.                                        
.             *End of loop over treatments below                                 
.             }                  
197.           
.          *End of loop over statistics below                            
.          }                     
198.       *End of loop over methods that use instruments, except for those using instruments partially applied            
>          
.       }                        
199. 
.       *Creating an indicator that the bootstrap run value is less or equal to the point estimate         
.       *It is used for the computation of confidence intervals using bootstrap percentiles         
.       foreach mth in $gnobias0 $gbias0 $gbiasnb {              
200.                   
.          *Loop over statistics                 
.          foreach st in $stat {         
201.             *Loop over treatment values         
.             forvalues t=1/$vtn {                       
202.             
.                *Loop over bounds         
.                foreach bb in lb ub {                   
203. 
.                   qui gen byte ib = v`st'_tr`t'_`bb'_`mth'<=b_`st'_tr`t'_`bb'_`mth'+tolr if ///         
>                      v`st'_tr`t'_`bb'_`mth'<.         
204. 
.                   *Calculating the percentage of draws that is less or equal to the bound                       
.                   qui su ib, meanonly                           
205.                   sca le_`st'_tr`t'_`bb'_`mth' = r(mean)                        
206.                   drop ib                       
207. 
.                   *Loop over p-values of confidence intervals                   
.                   forvalues npv=1/$ncicv {                  
208.       
.                      local pv: word `npv' of ${cicv}                 
209.                      local pv1 = round(`pv'*1000)                    
210. 
.                      *Calculating the z-stat                            
.                      local zstat = invnormal(1-((1-`pv')/2))                            
211. 
.                      *Calculationg the percentile corresponding to the lower bound                              
.                      if "`bb'"=="lb" {                  
212.                                         
.                        local pp = normal(2*invnormal(le_`st'_tr`t'_`bb'_`mth')-`zstat')                                 
213.                      }                  
214.                      else if "`bb'"=="ub" {                     
215.                                         
.                        local pp = normal(2*invnormal(le_`st'_tr`t'_`bb'_`mth')+`zstat')                                 
216.                      }                  
217.                                         
.                      *Calculating the value of the bootstrap runs corresponding to this percentile                       
>        
.                      if `pp'>tolr & `pp'<1-tolr {                       
218.                                         
.                         local pp1=100*`pp'                      
219.                         qui _pctile v`st'_tr`t'_`bb'_`mth', p(`pp1')                                    
220.                         sca bci`pv1'`st'_tr`t'_`bb'_`mth' = r(r1)                                       
221.                      }                  
222.                      else if `pp'<=tolr {                       
223.                                         
.                         qui su v`st'_tr`t'_`bb'_`mth', meanonly                         
224.                         sca bci`pv1'`st'_tr`t'_`bb'_`mth' = r(min)                      
225.                      }                  
226.                      else if `pp'>=1-tolr {                     
227.                                         
.                         qui su v`st'_tr`t'_`bb'_`mth', meanonly                         
228.                         sca bci`pv1'`st'_tr`t'_`bb'_`mth' = r(max)                      
229.                      }                  
230. 
.                      *For the nex method, we use these CIs in place of the         
.                      *Manski Imbens ones         
.                      if "`mth'"=="nex" {         
231.                               
.                          sca ci`pv1'`st'_tr`t'_`bb'_`mth' = bci`pv1'`st'_tr`t'_`bb'_`mth'          
232.                      }         
233. 
.                    *End of loop over p-values of confidence intervals                  
.                    }                              
234.               *End of loop over bounds below            
.               }                                         
235.             *End of loop over treatment values below                   
.             }                  
236.          *End of loop over statistcs below                     
.          }                     
237.       *End of loop over methods below                  
.       }                        
238. 
.       *Recording treatment effects             
.                        
.       *Loop over methods using instruments                     
.       foreach mth in $gbias0 {                 
239. 
.          assert $nm0>0                 
240.                        
.          *Recording the if conditions for the methods that do and do not require a minimum of 0 for the ATE              
>       
.          global mne ""                 
241.          global me ""                  
242.          forvalues k=1/$nm0 {                  
243. 
.             local gm: word `k' of ${m0}                        
244.             if `k'==1 {                        
245.                global mne "`mth'"!="`gm'"                      
246.                global me "`mth'"=="`gm'"                       
247.             }                  
248.             else if `k'>1 {                    
249.                global mne "${mne}" & "`mth'"!="`gm'"                   
250.                global me "${me}" | "`mth'"=="`gm'"                     
251.             }                  
252.          }                     
253. 
.          *Loop over statistics                 
.          foreach st in $stat {                 
254.                        
.             *Loop over treatments                              
.             *ATTENTION: do not merge this loop over treatments with the one above, which needs to be completed           
>       
.             *before this one                           
.             forvalues t=1/$vtn {                               
255.                                
.                *Adding the differences across values of the treatment                          
.                local jj = `t'+ 1                       
256.                forvalues m = `jj'/$vtn {                               
257.                                
.                   foreach bb in lb ub {                                
258.                                        
.                      *Calculating the point estimate after taking into account the bias correction                       
>    
.                       if "`bb'"=="lb" {                     
259. 
.                          if ("${mne}") {                     
260.                                    
.                             sca b_`st'_tr`m'_tr`t'_`bb'_`mth' = max(0,b_`st'_tr`m'_lb_`mth'-b_`st'_tr`t'_ub_`mth')       
>                     
261.                                      
.                             *We generate the difference in the bootstrap run results to calculate the standard error     
>        
.                             qui gen double vse = max(0,v`st'_tr`m'_lb_`mth'-v`st'_tr`t'_ub_`mth')         
262.                          }         
263.                          else if ("${me}") {                     
264. 
.                             sca b_`st'_tr`m'_tr`t'_`bb'_`mth' = b_`st'_tr`m'_lb_`mth'-b_`st'_tr`t'_ub_`mth'              
>              
265.                                 
.                             *We generate the difference in the bootstrap run results to calculate the standard error     
>        
.                             qui gen double vse = (v`st'_tr`m'_lb_`mth'-v`st'_tr`t'_ub_`mth')         
266.                          }                     
267.                       }                     
268. 
.                       else if "`bb'"=="ub" {                     
269.                          if ("${mne}") {                     
270.                                    
.                             sca b_`st'_tr`m'_tr`t'_`bb'_`mth' = max(0,b_`st'_tr`m'_ub_`mth'-b_`st'_tr`t'_lb_`mth')       
>      
271.                                 
.                             *We generate the difference in the bootstrap run results to calculate the standard error     
>        
.                             qui gen double vse = max(0,v`st'_tr`m'_ub_`mth'-v`st'_tr`t'_lb_`mth')         
272.                          }                     
273.                          else if ("${me}") {                     
274. 
.                             sca b_`st'_tr`m'_tr`t'_`bb'_`mth' = b_`st'_tr`m'_ub_`mth'-b_`st'_tr`t'_lb_`mth'            
275.                                   
.                             *We generate the difference in the bootstrap run results to calculate the standard error     
>        
.                             qui gen double vse = v`st'_tr`m'_ub_`mth'-v`st'_tr`t'_lb_`mth'         
276.                          }                     
277.                       }                     
278. 
.                       qui count if vse<.            
279.                       assert r(N)==${bsn}            
280. 
.                       sca se_`st'_tr`m'_tr`t'_`bb'_`mth'ol = se_`st'_tr`m'_tr`t'_`bb'_`mth'            
281.                       qui su vse             
282.                       sca se_`st'_tr`m'_tr`t'_`bb'_`mth' = r(sd)         
283.                                
.                       assert b_`st'_tr`m'_tr`t'_`bb'_`mth'>(${min${outn}}-${max${outn}})-${tolr} & ///                   
>         
>                          b_`st'_tr`m'_tr`t'_`bb'_`mth'<(${max${outn}}-${min${outn}})+${tolr}             
284.                       drop vse               
285.                                
.                   *End of loop over bounds below                               
.                   }                    
286.                *End of inner loop over treatments below                                
.                }                               
287.             *End of outer loop over treatments below                           
.             }                          
288.          *End of outer loop over statistics below                              
.          }                             
289.       *End of loop over methods using instruments below                        
.       }                        
290.       
.       *Calculating bootstrap percentile confidence intervals           
.       foreach mth in $gnobias0 $gbias0 $gbiasnb {              
291. 
.          assert $nm0>0                 
292.          *Recording the if conditions for the methods that do and do not require a minimum of 0 for the ATE           
>          
.          global mne ""                 
293.          global me ""                  
294.          forvalues k=1/$nm0 {                  
295. 
.             local gm: word `k' of ${m0}                        
296.             if `k'==1 {                        
297.                global mne "`mth'"!="`gm'"                      
298.                global me "`mth'"=="`gm'"                       
299.             }                  
300.             else if `k'>1 {                    
301.                global mne "${mne}" & "`mth'"!="`gm'"                   
302.                global me "${me}" | "`mth'"=="`gm'"                     
303.             }                  
304.          }                     
305. 
.          *Loop over statistics                 
.          foreach st in $stat {                 
306.                        
.             *Loop over treatments                              
.             *ATTENTION: do not merge this loop over treatments with the one above, which needs to be completed           
>       
.             *before this one                           
.             forvalues t=1/$vtn {                               
307.                                
.                *Adding the differences across values of the treatment                          
.                local jj = `t'+ 1                       
308.                forvalues m = `jj'/$vtn {                               
309.                                
.                   foreach bb in lb ub {                                
310. 
.                      *Creating an indicator that the bootstrap run value is less or equal to the point estimate         
.                      qui gen byte ib = 0 if v`st'_tr`t'_`bb'_`mth'<.         
311. 
.                      *Calculating the point estimate after taking into account the bias correction                       
>    
.                      if "`bb'"=="lb" {                     
312. 
.                         if ("${mne}") {                     
313.                                    
.                            qui replace ib = 1 if max(0,v`st'_tr`m'_lb_`mth'-v`st'_tr`t'_ub_`mth')< ///         
>                               b_`st'_tr`m'_tr`t'_lb_`mth'+tolr & v`st'_tr`t'_`bb'_`mth'<.         
314.                                   
.                            *Calculating the percentage of draws that is less or equal to the bound              
.                            qui su ib, meanonly                  
315.                            sca le_`st'_tr`m'_tr`t'_`bb'_`mth' = r(mean)                 
316.                            drop ib              
317. 
.                            *Loop over p-values of confidence intervals          
.                            forvalues npv=1/$ncicv {         
318.       
.                               local pv: word `npv' of ${cicv}          
319.                               local pv1 = round(`pv'*1000)         
320. 
.                               *Calculating the z-stat                   
.                               local zstat = invnormal(1-((1-`pv')/2))                  
321. 
.                               *Calculationg the percentile corresponding to the lower bound                     
.                               local pp = normal(2*invnormal(le_`st'_tr`m'_tr`t'_`bb'_`mth')-`zstat')                    
322. 
.                               *Calculating the value of the bootstrap runs corresponding to this percentile              
>        
.                               qui gen double dci=max(0,v`st'_tr`m'_lb_`mth'-v`st'_tr`t'_ub_`mth')         
323.                               
.                               if `pp'>tolr & `pp'<1-tolr {              
324.                                         
.                                  local pp1=100*`pp'             
325.                                  qui _pctile dci, p(`pp1')                              
326.                                  sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = r(r1)                                
327.                               }         
328.                               else if `pp'<=tolr {              
329.                                         
.                                  qui su dci, meanonly           
330.                                  sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = r(min)               
331.                               }         
332.                               else if `pp'>=1-tolr {            
333.                                         
.                                  qui su dci, meanonly           
334.                                  sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = r(max)               
335.                               }         
336.                               drop dci         
337. 
.                            *End of loop over p-values of confidence intervals         
.                            }                     
338.                         }         
339.                         else if ("${me}") {                     
340. 
.                            qui replace ib = 1 if v`st'_tr`m'_lb_`mth'-v`st'_tr`t'_ub_`mth'< ///         
>                               b_`st'_tr`m'_tr`t'_lb_`mth'+tolr         
341. 
.                            *Calculating the percentage of draws that is less or equal to the bound              
.                            qui su ib, meanonly                  
342.                            sca le_`st'_tr`m'_tr`t'_`bb'_`mth' = r(mean)                 
343.                            drop ib              
344. 
.                            *Loop over p-values of confidence intervals          
.                            forvalues npv=1/$ncicv {         
345.       
.                               local pv: word `npv' of ${cicv}          
346.                               local pv1 = round(`pv'*1000)         
347. 
.                               *Calculating the z-stat                   
.                               local zstat = invnormal(1-((1-`pv')/2))                   
348.                               *Calculationg the percentile corresponding to the lower bound                     
.                               local pp = normal(2*invnormal(le_`st'_tr`m'_tr`t'_`bb'_`mth')-`zstat')                    
349. 
.                               *Calculating the value of the bootstrap runs corresponding to this percentile              
>        
.                               qui gen double dci=v`st'_tr`m'_lb_`mth'-v`st'_tr`t'_ub_`mth'              
350.                               if `pp'>tolr & `pp'<1-tolr {              
351.                                         
.                                 local pp1=100*`pp'              
352.                                 qui _pctile dci, p(`pp1')                               
353.                                 sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = r(r1)                                 
354.                              }          
355.                              else if `pp'<=tolr {               
356.                                         
.                                 qui su dci, meanonly            
357.                                 sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = r(min)                
358.                              }          
359.                              else if `pp'>=1-tolr {             
360.                                         
.                                 qui su dci, meanonly            
361.                                 sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = r(max)                
362.                              }          
363.                              drop dci         
364. 
.                              *For the nex method, we use these CIs in place of the         
.                              *Manski Imbens ones         
.                              if "`mth'"=="nex" {         
365.                               
.                                  sca ci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth'          
366.                              }         
367. 
.                            *End of loop over p-values of confidence intervals         
.                            }                     
368.                         }                     
369.                      }                     
370. 
.                      else if "`bb'"=="ub" {                     
371.                         if ("${mne}") {                     
372.                                    
.                            qui replace ib = 1 if max(0,v`st'_tr`m'_ub_`mth'-v`st'_tr`t'_lb_`mth')< ///         
>                               b_`st'_tr`m'_tr`t'_ub_`mth'+tolr         
373. 
.                            *Calculating the percentage of draws that is less or equal to the bound              
.                            qui su ib, meanonly                  
374.                            sca le_`st'_tr`m'_tr`t'_`bb'_`mth' = r(mean)                 
375.                            drop ib              
376. 
.                            *Loop over p-values of confidence intervals          
.                            forvalues npv=1/$ncicv {         
377.       
.                               local pv: word `npv' of ${cicv}          
378.                               local pv1 = round(`pv'*1000)         
379. 
.                               *Calculating the z-stat                   
.                               local zstat = invnormal(1-((1-`pv')/2))                   
380.                               *Calculationg the percentile corresponding to the lower bound                     
.                               local pp = normal(2*invnormal(le_`st'_tr`m'_tr`t'_`bb'_`mth')+`zstat')                    
381. 
.                               *Calculating the value of the bootstrap runs corresponding to this percentile              
>        
.                               qui gen double dci=max(0,v`st'_tr`m'_ub_`mth'-v`st'_tr`t'_lb_`mth')         
382.                               if `pp'>tolr & `pp'<1-tolr {              
383.                                         
.                                  local pp1=100*`pp'             
384.                                  qui _pctile dci, p(`pp1')                              
385.                                  sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = r(r1)                                
386.                               }         
387.                               else if `pp'<=tolr {              
388.                                         
.                                  qui su dci, meanonly           
389.                                  sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = r(min)               
390.                               }         
391.                               else if `pp'>=1-tolr {            
392.                                         
.                                  qui su dci, meanonly           
393.                                  sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = r(max)               
394.                               }         
395.                               drop dci         
396. 
.                            *End of loop over p-values of confidence intervals         
.                            }                     
397.                         }                     
398.                         else if ("${me}") {                     
399. 
.                            qui replace ib = 1 if v`st'_tr`m'_ub_`mth'-v`st'_tr`t'_lb_`mth'< ///         
>                               b_`st'_tr`m'_tr`t'_ub_`mth'+tolr         
400. 
.                            *Calculating the percentage of draws that is less or equal to the bound              
.                            qui su ib, meanonly                  
401.                            sca le_`st'_tr`m'_tr`t'_`bb'_`mth' = r(mean)                 
402.                            drop ib              
403. 
.                            *Loop over p-values of confidence intervals          
.                            forvalues npv=1/$ncicv {         
404.       
.                               local pv: word `npv' of ${cicv}          
405.                               local pv1 = round(`pv'*1000)         
406. 
.                               *Calculating the z-stat                   
.                               local zstat = invnormal(1-((1-`pv')/2))                   
407.                               *Calculationg the percentile corresponding to the lower bound                     
.                               local pp = normal(2*invnormal(le_`st'_tr`m'_tr`t'_`bb'_`mth')+`zstat')                    
408. 
.                               *Calculating the value of the bootstrap runs corresponding to this percentile              
>        
.                               qui gen double dci=v`st'_tr`m'_ub_`mth'-v`st'_tr`t'_lb_`mth'              
409.                               if `pp'>tolr & `pp'<1-tolr {              
410.                                         
.                                  local pp1=100*`pp'             
411.                                  qui _pctile dci, p(`pp1')                              
412.                                  sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = r(r1)                                
413.                               }         
414.                               else if `pp'<=tolr {              
415.                                         
.                                  qui su dci, meanonly           
416.                                  sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = r(min)               
417.                               }         
418.                               else if `pp'>=1-tolr {            
419.                                         
.                                  qui su dci, meanonly           
420.                                  sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = r(max)               
421.                               }         
422.                               drop dci         
423. 
.                               *For the nex method, we use these CIs in place of the         
.                               *Manski Imbens ones         
.                               if "`mth'"=="nex" {         
424.                               
.                                  sca ci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth'          
425.                               }         
426. 
.                             *End of loop over p-values of confidence intervals         
.                             }                     
427.                          }                     
428.                       }                     
429. 
.                   *End of loop over bounds below                               
.                   }                    
430.                *End of inner loop over treatments below                                
.                }                               
431.             *End of outer loop over treatments below                           
.             }                          
432.          *End of outer loop over statistics below                              
.          }                             
433.       *End of loop over methods using instruments below                        
.       }                        
434. 
.       *Increasing the nubmer of observations in order to accommodate a new variable            
.       local newobs=((${chigh}-${clow})*${incc}) + 1            
435.       qui set obs `newobs'            
436. 
. 
.       *Imbens Manski (2004) confidence intervals         
.                 
.       *Loop over statistics         
.       forvalues stn=1/$stat_n {         
437.          local st: word `stn' of ${stat}         
438.          local fst: word 1 of ${stat}         
439.                   
.          *Loop over methods            
.          forvalues mnn=1/$gmethod2_n {            
440.             local mth: word `mnn' of ${gmethod2}         
441.             local fmth: word 1 of ${gmethod2}         
442. 
.             *Loop over values of the treatment            
.             forvalues t=1/$vtn2 {            
443.                             
.                 local j1: word `t' of ${vt}            
444.                 local jj = `t'+ 1                
445.                                
. 
.                 forvalues m = `jj'/$vtn {               
446. 
.                    *Condition excluding the nex method            
.                    if ! ("`mth'"=="nex") {            
447. 
.                       *If the std errors of the lower and upper bounds are not both equal to zero then we             
.                       *calculate a coefficient for the confidence interval of the point estimate            
.                       if ! (se_`st'_tr`m'_tr`t'_lb_`mth'<tolr & se_`st'_tr`m'_tr`t'_ub_`mth'<tolr) {            
448. 
.                          *Loop over p-values of confidence intervals             
.                          forvalues npv=1/$ncicv {            
449.                             
.                             local pv: word `npv' of ${cicv}             
450.                             local pv1 = `pv'*1000            
451. 
. 
.                            *Generating a variable that takes values between the upper and lower bounds of the coefficient
>                   
.                            *for the calculation of the confidence interval in predefined increments                   
.                            qui set obs `newobs'                  
452.                            qui gen double d1 = ${clow} + (_n-1)/${incc} if _n<=((${chigh}-${clow})*${incc}) + 1       
>            
453. 
.                             *We calculate the difference between two cumulative normals            
.                             qui gen double dcn = normal(d1 + ((b_`st'_tr`m'_tr`t'_ub_`mth' - b_`st'_tr`m'_tr`t'_lb_`mth')
> / ///            
>                                max(se_`st'_tr`m'_tr`t'_lb_`mth',se_`st'_tr`m'_tr`t'_ub_`mth'))) - normal(-d1)            
454.                                   
.                             *Generating a variable equal to the difference between the difference of the two cumulative n
> ormals and            
.                             *the required coverage            
. 
.                             *If the bounds do not cross then the difference is put equal to alpha         
.                             if ! (b_`st'_tr`m'_tr`t'_ub_`mth'+${tolr}<b_`st'_tr`m'_tr`t'_lb_`mth') {          
455.                                
.                                qui gen double d2 = abs(dcn - `pv') if dcn~=.                   
456.                             }         
457.                             *If the bounds cross then the difference is put equal to alpha/2         
.                             else if (b_`st'_tr`m'_tr`t'_ub_`mth'+${tolr}<b_`st'_tr`m'_tr`t'_lb_`mth') {          
458. 
.                                qui gen double d2 = abs(dcn - (`pv'/2)) if dcn~=.                   
459.                             }         
460. 
.                             *Finding which value is closest to the required coverage            
.                             qui su d2 if dcn~=., meanonly            
461.                             sca tt = r(min)            
462.                             qui gen double d3 = d1 if abs(d2-tt)<tolr & dcn~=.            
463.                             qui count if d3~=. & dcn~=.            
464.                             *For methods for which the upper bound is at least as high as the lower bound, then       
>   
.                             *the value should be unique         
.                             if b_`st'_tr`m'_tr`t'_ub_`mth' - b_`st'_tr`m'_tr`t'_lb_`mth'>=-${tolr} {         
465.                                assert r(N)==1 & tt<1e-3            
466.                             }         
467.                             qui su d3 if dcn~=., meanonly            
468.                             sca co`st'_tr`m'_tr`t'_`mth'`pv1' = r(max)            
469.                             drop d1 d2 d3 dcn            
470.                             sca drop tt            
471.                             drop _all         
472. 
.                          *End of loop over p-values of confidence intervals         
.                          }         
473.                       }         
474.                                
.                       *If the std errors of both the lower and upper bounds are zero then we put the coefficient         
.                       *of the confidence interval equal to zero for the Manski Imbens bound, and the          
.                       *confidence interval bound equal to the estimate.         
.                       if (se_`st'_tr`m'_tr`t'_lb_`mth'<tolr & se_`st'_tr`m'_tr`t'_ub_`mth'<tolr) {         
475.                                   
. 
.                          *Loop over p-values of confidence intervals          
.                          forvalues npv=1/$ncicv {         
476.                             
.                             local pv: word `npv' of ${cicv}          
477.                             local pv1 = `pv'*1000         
478. 
.                             sca co`st'_tr`m'_tr`t'_`mth'`pv1' = 0         
479.                                   
.                           foreach bb in lb ub {         
480.                                         
.                              sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = b_`st'_tr`m'_tr`t'_`bb'_`mth'            
481.                           }             
482.                                   
.                          *End of loop over p-values of conficence intervals         
.                          }         
483.                       }         
484.                       *Loop over p-values of confidence intervals          
.                       forvalues npv=1/$ncicv {         
485.                             
.                          local pv: word `npv' of ${cicv}          
486.                          local pv1 = `pv'*1000         
487. 
.                          assert co`st'_tr`m'_tr`t'_`mth'`pv1'~=.            
488.                       *End of loop over p-values of conficence intervals         
.                       }         
489.                    *End of condition excluding the nex method         
.                    }         
490.                 *End of inner loop over treatment values            
.                 }            
491.             *End of outer loop over treatment values            
.             }                
492. 
.             *Adding the levels of the means across treatments                    
. 
.             *Loop over values of the treatment            
.             forvalues t=1/$vtn {           
493.                          
.                 if ! ("`mth'"=="nex") {         
494.                             
.                    *If the std errors of the lower and upper bounds are not both equal to zero then we          
.                    *calculate a coefficient for the confidence interval of the point estimate         
.                    if ! (se_`st'_tr`t'_lb_`mth'==0 & se_`st'_tr`t'_ub_`mth'==0) {         
495. 
.                       *Loop over p-values of confidence intervals          
.                       forvalues npv=1/$ncicv {         
496. 
.                          local pv: word `npv' of ${cicv}          
497.                          local pv1 = `pv'*1000         
498. 
.                         *Generating a variable that takes values between the upper and lower bounds of the coefficient   
>                
.                         *for the calculation of the confidence interval in predefined increments                   
.                         qui set obs `newobs'                  
499.                         qui gen double d1 = ${clow} + (_n-1)/${incc} if _n<=((${chigh}-${clow})*${incc}) + 1          
>         
500.                                
.                          *We calculate the difference between two cumulative normals            
.                          qui gen double dcn = normal(d1 + ((b_`st'_tr`t'_ub_`mth' - b_`st'_tr`t'_lb_`mth')/ ///          
>      
>                             max(se_`st'_tr`t'_lb_`mth',se_`st'_tr`t'_ub_`mth'))) - normal(-d1)               
501.                                   
.                          *Generating a variable equal to the difference between the difference of the two cumulative norm
> als and               
.                          *the required coverage               
.                             
.                          *If the bounds do not cross then the difference is put equal to alpha         
.                          if ! (b_`st'_tr`t'_ub_`mth'+${tolr}<b_`st'_tr`t'_lb_`mth') {          
502.                                
.                             qui gen double d2 = abs(dcn - `pv') if dcn~=.                   
503.                          }         
504.                          *If the bounds cross then the difference is put equal to alpha/2         
.                          else if (b_`st'_tr`t'_ub_`mth'+${tolr}<b_`st'_tr`t'_lb_`mth') {          
505. 
.                             qui gen double d2 = abs(dcn - (`pv'/2)) if dcn~=.                   
506.                          }         
507. 
.                          *Finding which value is closest to the required coverage               
.                          qui su d2 if dcn~=., meanonly               
508.                          sca tt = r(min)               
509.                          qui gen double d3 = d1 if abs(d2-tt)<tolr & dcn~=.               
510.                          qui count if d3~=. & dcn~=.               
511.                          *For methods for which the upper bound is at least as high as the lower bound, then         
.                          *the value should be unique         
.                          if b_`st'_tr`t'_ub_`mth' - b_`st'_tr`t'_lb_`mth'>=-${tolr} {         
512.                             assert r(N)==1 & tt<1e-3               
513.                          }         
514.                          qui su d3 if dcn~=., meanonly               
515.                          sca co`st'_tr`t'_`mth'`pv1' = r(max)               
516.                                   
.                          drop d1 d2 d3 dcn               
517.                          sca drop tt               
518.                          drop _all         
519. 
.                       *End of loop over p-values of conficence intervals         
.                       } 
520.                    }         
521. 
.                    *If the std errors of both the lower and upper bounds are zero then we put the coefficient         
.                    *of the confidence interval equal to zero for the Manski Imbens bound, and the          
.                    *confidence interval bound equal to the estimate.         
.                    else if (se_`st'_tr`t'_lb_`mth'<tolr & se_`st'_tr`t'_ub_`mth'<tolr) {         
522.                                
.                       *Loop over p-values of confidence intervals          
.                       forvalues npv=1/$ncicv {         
523.                             
.                          local pv: word `npv' of ${cicv}          
524.                          local pv1 = `pv'*1000         
525.  
.                          sca co`st'_tr`t'_`mth'`pv1' = 0         
526. 
.                        foreach bb in lb ub {            
527.                                         
.                           sca bci`pv1'`st'_tr`t'_`bb'_`mth' = b_`st'_tr`t'_`bb'_`mth'           
528.                        }                
529.                       *End of loop over p-values of conficence intervals         
.                       }         
530.                    }         
531.                    *Loop over p-values of confidence intervals          
.                    forvalues npv=1/$ncicv {         
532.                             
.                       local pv: word `npv' of ${cicv}          
533.                       local pv1 = `pv'*1000         
534. 
.                       assert co`st'_tr`t'_`mth'`pv1'~=.         
535.                    *End of loop over p-values of conficence intervals         
.                    }         
536.                             
.                 *End of condition excluding the nex method         
.                 }       
537.             *End of loop over values of the treatment below         
.             }            
538.          *End of loop over methods below          
.          }                         
539.       *End of loop over statistics below          
.       }                         
540. 
.       *Assembling the results
. 
.       *Loop over statistics         
.       forvalues stn=1/$stat_n {         
541.          local st: word `stn' of ${stat}         
542.          local fst: word 1 of ${stat}         
543.                   
.          *Loop over methods            
.          forvalues mnn=1/$gmethod2_n {            
544.             local mth: word `mnn' of ${gmethod2}         
545.             local fmth: word 1 of ${gmethod2}         
546. 
.             *Local of the rownames of the output matrix               
.             local rl ""            
547. 
.             *Loop over values of the treatment            
.             forvalues t=1/$vtn2 {            
548.                             
.                 local j1: word `t' of ${vt}            
549.                 local jj = `t'+ 1                
550.                                
.                 forvalues m = `jj'/$vtn {               
551. 
.                    local j2: word `m' of ${vt}             
552.                    local rl `rl' `st'_`mth'_v`j2'v`j1'               
553. 
.                 *End of inner loop over treatment values            
.                 }            
554.             *End of outer loop over treatment values            
.             }                
555. 
.             *Adding the levels of the means across treatments                    
.             *Putting a blank line to separate results for differences from those for levels              
. 
.             if "`rl'"~="" {         
556.                 local rl `rl' blank            
557.             }         
558. 
.             *Loop over values of the treatment            
.             forvalues t=1/$vtn {           
559. 
.                 local j1: word `t' of ${vt}         
560.                 local rl `rl'  `st'_`mth'_v`j1'            
561. 
.             *End of loop over values of the treatment below         
.             }            
562. 
.             foreach mth2 in $gmethod $gbiasnb {                  
563.                 if "`mth'"=="`mth2'" {         
564.                    forvalues t=1/$vtn {            
565.                                   
.                       local j1: word `t' of ${vt}         
566.                       local rl `rl' pcr_`mth2'_v`j1'         
567.                    }         
568.                 }         
569.             }         
570. 
.             local rl `rl' blank            
571.                   
.             *Number of rows of the output matrix            
.             local rln: word count `rl'            
572.                                   
.             *Output matrix                     
.             *For the nex method we also input the number of observations                
.             if ("`st'"=="m" & "`mth'"=="nex") {                 
573.                local rln = `rln'+1              
574.                local rl `rl' nobs_nomiv                 
575.                                 
.                if ("${minstr1}"=="") {                  
576.                   local rln = `rln'+1                   
577.                   local rl `rl' blank                   
578.                }                
579.                                 
.                else if ("${minstr1}"!="") {                     
580.                  local rln = `rln'+2                    
581.                  local rl `rl' nobs_miv1 blank                  
582.                }                
583.             }                  
584.             mat Aout`out'_`mth'_t`tv'_`st' = J(`rln',${cln}+1,.)                  
585. 
.            mat colnames Aout`out'_`mth'_t`tv'_`st' = ${cl}                      
586.            mat rownames Aout`out'_`mth'_t`tv'_`st' = `rl'               
587. 
.             *Filling in the output matrix               
.             local cv = 0                
588. 
.             *Recording the if conditions for the methods that do and do not require a minimum of 0 for the ATE           
.             global mne ""                
589.             global me ""                 
590.             forvalues k=1/$nm0 {                 
591.                      
.                local gm: word `k' of ${m0}               
592.                if `k'==1 {               
593.                         
.                   global mne "`mth'"!="`gm'"             
594.                   global me "`mth'"=="`gm'"              
595.                }                 
596.                else if `k'>1 {           
597.                         
.                   global mne "${mne}" & "`mth'"!="`gm'"          
598.                   global me "${me}" | "`mth'"=="`gm'"            
599.                }                 
600.             }            
601. 
.            *Putting in first the results of the differences                                     
.            forvalues t=1/$vtn2 {                
602.                 local jj = `t'+ 1               
603.                                 
.                 forvalues m = `jj'/$vtn {               
604.                    local cv = `cv'+1                    
605.                    local plc = 0        
606.                                 
.                    *Lower bound                 
.                    local plc = `plc'+1          
607.                    mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']=b_`st'_tr`m'_tr`t'_lb_`mth'               
608. 
.                    *Upper bound                 
.                    local plc = `plc'+1          
609.                    mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']=b_`st'_tr`m'_tr`t'_ub_`mth'               
610. 
.                    *Loop over p-values of confidence intervals          
.                    forvalues npv=1/$ncicv {         
611.                             
.                       local pv: word `npv' of ${cicv}          
612.                       local pv1 = round(`pv'*1000)         
613.                       *local pv0 = round(1000*(1-`pv'))         
.                                         
.                       *Bootstrap percentile method              
.                                 
.                       *Low boundary of the confidence interval                  
.                                 
.                       local plc = `plc'+1       
614.                       if ("${mne}") {           
615. 
.                          mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc'] = ///       
>                             min(max(0,bci`pv1'`st'_tr`m'_tr`t'_lb_`mth'),${max${outn}}-${min${outn}})           
616.                       }                 
617.                       else if ("${me}") {       
618.                                 
.                         mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']= ///         
>                             min(max(${min${outn}}-${max${outn}},bci`pv1'`st'_tr`m'_tr`t'_lb_`mth'), ${max${outn}}-${min${
> outn}})                
619.                       }                 
620. 
.                       *Upper boundary of the confidence interval                
. 
.                       local plc = `plc'+1       
621.                       if ("${mne}") {           
622. 
.                          mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']= ///        
>                             min(max(0,bci`pv1'`st'_tr`m'_tr`t'_ub_`mth'),${max${outn}}-${min${outn}})           
623.                       }                 
624.                       else if ("${me}") {       
625.                                 
.                          mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']= ///        
>                             min(max(${min${outn}}-${max${outn}},bci`pv1'`st'_tr`m'_tr`t'_ub_`mth'), ${max${outn}}-${min${
> outn}})        
626.                       }         
627. 
.                      *Imbens-Manski (2004) confidence intervals         
.                                 
.                       *Low boundary of the confidence interval                  
.                       local plc = `plc'+1       
628.                       if ("${mne}") {           
629. 
.                          mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc'] = ///       
>                             min(max(0,b_`st'_tr`m'_tr`t'_lb_`mth' - ///                 
>                             co`st'_tr`m'_tr`t'_`mth'`pv1'*se_`st'_tr`m'_tr`t'_lb_`mth'), ///            
>                             ${max${outn}}-${min${outn}})                
630.                       }                 
631. 
.                       else if ("${me}") {       
632.                                 
.                          if ("`mth'"~="nex") {          
633.                                         
.                             mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']= ///             
>                                min(max(${min${outn}}-${max${outn}},b_`st'_tr`m'_tr`t'_lb_`mth' - ///                    
>                                co`st'_tr`m'_tr`t'_`mth'`pv1'*se_`st'_tr`m'_tr`t'_lb_`mth'), ///         
>                                ${max${outn}}-${min${outn}})             
634.                          }              
635.                          else if ("`mth'"=="nex") {             
636.                                         
.                             mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']= ///             
>                                min(max(${min${outn}}-${max${outn}},ci`pv1'`st'_tr`m'_tr`t'_lb_nex), ///         
>                                ${max${outn}}-${min${outn}})             
637.                          }              
638.                       }                 
639. 
.                       *Upper boundary of the confidence interval                
.                                 
.                       local plc = `plc'+1       
640.                       if ("${mne}") {           
641. 
.                          mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']= ///        
>                             min(max(0,b_`st'_tr`m'_tr`t'_ub_`mth' + ///         
>                             co`st'_tr`m'_tr`t'_`mth'`pv1'*se_`st'_tr`m'_tr`t'_ub_`mth'), ///            
>                             ${max${outn}}-${min${outn}})                
642.                       }                 
643.                       else if ("${me}") {       
644.                                 
.                          if ("`mth'"!="nex") {  
645.                                 
.                             mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']= ///     
>                                min(max(${min${outn}}-${max${outn}},b_`st'_tr`m'_tr`t'_ub_`mth' + ///            
>                                co`st'_tr`m'_tr`t'_`mth'`pv1'*se_`st'_tr`m'_tr`t'_ub_`mth'), ///         
>                                ${max${outn}}-${min${outn}})             
646.                             sca drop co`st'_tr`m'_tr`t'_`mth'`pv1'              
647.                                     
.                         }            
648.                                 
.                          else if ("`mth'"=="nex") {             
649.                                         
.                             mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']= ///             
>                                min(max(${min${outn}}-${max${outn}},ci`pv1'`st'_tr`m'_tr`t'_ub_nex), ///         
>                                ${max${outn}}-${min${outn}})             
650.                          }              
651.                       }         
652. 
.                    *End of loop over p-values of conficence intervals         
.                    }         
653. 
.               *End of inner loop over treatment values                  
.               }                 
654.            *End of outer loop over treatment values             
.            }                    
655. 
.            *Putting in afterwards the results of the levels and the percentage of boostrap runs         
.            *for which the lower bound is higher than the upper bound            
.            *Loop over rows of the matrix        
.            if `cv'>0 {          
656.                 local cv = `cv'+1               
657.            }            
658.            forvalues t=1/$vtn {                 
659.                         
.                 local cv = `cv'+1               
660.                 local plc = 0                                           
661.                         
.                 *Lower bound                    
.                 local plc = `plc'+1             
662.                 mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']=b_`st'_tr`t'_lb_`mth'                
663. 
.                 *Upper bound                    
.                 local plc = `plc'+1             
664.                 mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']=b_`st'_tr`t'_ub_`mth'                
665. 
.                 *Loop over p-values of confidence intervals             
.                 forvalues npv=1/$ncicv {            
666.                             
.                    local pv: word `npv' of ${cicv}             
667.                    local pv1 = round(`pv'*1000)            
668.                    *local pv0 = round(1000*(1-`pv'))            
.                                         
.                    *Bootstrap percentile method                                         
.                                 
.                    *Low boundary of the confidence interval                     
. 
.                    local plc = `plc'+1          
669.                    mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc'] = ///             
>                       min(${max${outn}},max(${min${outn}},bci`pv1'`st'_tr`t'_lb_`mth'))                 
670. 
.                    *Upper boundary of the confidence interval                   
.                    local plc = `plc'+1          
671.                    mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc'] = ///     
>                       max(${min${outn}},min(${max${outn}},bci`pv1'`st'_tr`t'_ub_`mth'))                 
672. 
.                    *Imbens-Manski (2004) confidence intervals           
.                                 
.                    *Low boundary of the confidence interval                     
.                    local plc = `plc'+1          
673.                    if ("`mth'"!="nex") {                
674.                                 
.                       mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc'] = ///          
>                          min(${max${outn}},max(${min${outn}}, b_`st'_tr`t'_lb_`mth' - ///                       
>                          co`st'_tr`t'_`mth'`pv1'*se_`st'_tr`t'_lb_`mth'))                       
675.                   }            
676.                    else if ("`mth'"=="nex") {           
677.                                         
.                       mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']= ///           
>                          min(${max${outn}},max(${min${outn}},ci`pv1'`st'_tr`t'_lb_nex))                 
678.                    }            
679. 
.                    *Upper boundary of the confidence interval                   
.                    local plc = `plc'+1          
680.                    if ("`mth'"!="nex") {                
681.                       mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc'] = ///          
>                          max(${min${outn}},min(${max${outn}},b_`st'_tr`t'_ub_`mth' + ///                
>                          co`st'_tr`t'_`mth'`pv1'*se_`st'_tr`t'_ub_`mth'))                       
682.                                          
.                          sca drop co`st'_tr`t'_`mth'`pv1'                       
683. 
.                   }                                    
684.                    else if ("`mth'"=="nex") {           
685.                       mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc'] = ///          
>                          max(${min${outn}},min(${max${outn}},ci`pv1'`st'_tr`t'_ub_nex))                 
686.                   }                                    
687. 
.                    *assert Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']>${min${outn}}-tolr                    
. 
.                 *End of loop over p-values of confidence intervals         
.                 }         
688. 
.            *End of loop over rows of the matrix                 
.            }            
689. 
.            *Putting in the percentage of bootstrap draws for which the upper bound is lower than                
.            *the lower bound     
.            foreach mth2 in $gmethod $gbiasnb {          
690.                         
.                 if "`mth'"=="`mth2'" {          
691.                         
.                    forvalues t=1/$vtn {                 
692.                       local cv = `cv'+1                 
693.             
.                       mat Aout`out'_`mth'_t`tv'_`st'[`cv',1] = ${pcr_`st'_tr`t'_`mth2'}         
694.                    }    
695.                 }       
696.            }                                    
697. 
.             *Adding the number of observations for the nex method         
.             if ("`st'"=="m" & "`mth'"=="nex") {         
698.                      
.                local cv = `cv'+2               
699.                mat Aout`out'_`mth'_t`tv'_`st'[`cv',1]=nobs_nomiv         
700.                         
.                if ("${minstr1}"!="") {         
701.                   local cv = `cv'+1               
702.                   mat Aout`out'_`mth'_t`tv'_`st'[`cv',1]=nobs_miv1         
703.                }         
704.             }            
705. 
.             *Checking that the number of cells matches the number of results               
.             assert `cv' == `rln'-1              
706. 
.             *Stacking vertically the results over methods
.             if `mnn'==1 {
707.                mat A_out`out'_t`tv'_`st' = Aout`out'_`mth'_t`tv'_`st'
708.             }
709.             else if `mnn'!=1 {
710.                mat A_out`out'_t`tv'_`st' = ///
>                   (A_out`out'_t`tv'_`st'\Aout`out'_`mth'_t`tv'_`st')
711.             }
712.             mat drop Aout`out'_`mth'_t`tv'_`st'
713. 
.         
.          *End of loop over methods below 
.          } 
714. 
.          *Stacking vertically the results over statistics   
.          if `stn'==1 {   
715.             mat A_out`out'_t`tv' = A_out`out'_t`tv'_`st'   
716.          }   
717.          else if `stn'!=1 {   
718.             mat A_out`out'_t`tv' = ///   
>                (A_out`out'_t`tv'\A_out`out'_t`tv'_`st')   
719.          }   
720.          mat drop A_out`out'_t`tv'_`st'   
721. 
.       *End of loop over statistics below         
.       }   
722. 
.       erase "${projf}/Data/temp1.dta"      
723.       erase "${projf}/Data/tempres.dta"      
724.       erase "${projf}/Data/tempres1.dta"      
725. 
.       *Stacking vertically the results over treatment variables   
.       if "${treat}"=="`fltv'" {   
726.          mat A_out`out' = A_out`out'_t`tv'   
727.       }   
728.       else if "${treat}"!="`fltv'" {   
729.          mat A_out`out' = (A_out`out'\A_out`out'_t`tv')   
730.       }   
731.       mat drop A_out`out'_t`tv'   
732. 
.    *End of loop over treatment variables below   
.    }   
733. 
.    *Joining results horizontally over outcomes   
.    if `out'==1 {   
734.       mat A = A_out`out'    
735.    }   
736.    else if `out'>1 {   
737.       mat A = (A,A_out`out')   
738.    }   
739.    mat drop A_out`out'   
740. 
. *End of loop over outcomes below
. }
Outcome w005_hpconch_nyr, treat q3_hmsptri_sqcgr, miv1 q10_hvsptri_incgr
Bootstrap replicates
Bootstrap run no. 200
Bootstrap run no. 400
Bootstrap run no. 600
Bootstrap run no. 800
Bootstrap run no. 1000

. 
. mat list A

A[58,11]
                        lb            ub    95_bci_low   95_bci_high   95_imci_low  95_imci_high    90_bci_low
  m_nex_v2v1     .00369071     .00369071     .00130544     .00586942     .00130544     .00586942     .00164276
  m_nex_v3v1     .02830266     .02830266     .01579722     .04198716     .01579722     .04198716     .01738462
  m_nex_v3v2     .02461195     .02461195     .01165986     .03874082     .01165986     .03874082     .01357192
       blank             .             .             .             .             .             .             .
    m_nex_v1     .00451939     .00451939     .00377827     .00531862     .00377827     .00531862     .00391004
    m_nex_v2      .0082101      .0082101     .00598643     .01025745     .00598643     .01025745       .006335
    m_nex_v3     .03282205     .03282205     .02057834     .04655901     .02057834     .04655901     .02218763
  pcr_nex_v1             0             .             .             .             .             .             .
  pcr_nex_v2             0             .             .             .             .             .             .
  pcr_nex_v3             0             .             .             .             .             .             .
       blank             .             .             .             .             .             .             .
  nobs_nomiv          2791             .             .             .             .             .             .
   nobs_miv1          2791             .             .             .             .             .             .
       blank             .             .             .             .             .             .             .
    m_n_v2v1    -.88197042     .87161881    -.89773279      .8875349    -.89534845     .88552491    -.89503636
    m_n_v3v1    -.87278702     .87927783    -.88797677     .89483631    -.88643563     .89286537     -.8856004
    m_n_v3v2    -.83875175     .85559417    -.85500082     .87087041    -.85161321      .8683937    -.85252892
       blank             .             .             .             .             .             .             .
      m_n_v1    -.34695757     .55869652    -.35787796     .57615883    -.35588864     .57280677     -.3560537
      m_n_v2     -.3232739     .52466124    -.33511773     .54292536    -.33310322     .53968843    -.33397138
      m_n_v3    -.31409051     .53232027    -.32780653     .55089722    -.32573944     .54768818    -.32606125
    pcr_n_v1             0             .             .             .             .             .             .
    pcr_n_v2             0             .             .             .             .             .             .
    pcr_n_v3             0             .             .             .             .             .             .
       blank             .             .             .             .             .             .             .
   m_nr_v2v1             0     .60393907             0     .61145686             0     .61015815             0
   m_nr_v3v1             0     .87927783     6.939e-18     .89483631             0     .89286537     6.939e-18
   m_nr_v3v2             0     .70255174             0     .70974916             0     .70875557             0
       blank             .             .             .             .             .             .             .
     m_nr_v1    -.34695757     .01567802    -.35787796     .02066407    -.35588864     .01976459     -.3560537
     m_nr_v2    -.17023147     .25698151    -.18228274     .27412645    -.18036018     .27120928    -.18027739
     m_nr_v3     .01567802     .53232027     .01125195     .55089722     .01159146     .54768818     .01187135
   pcr_nr_v1             0             .             .             .             .             .             .
   pcr_nr_v2             0             .             .             .             .             .             .
   pcr_nr_v3             0             .             .             .             .             .             .
       blank             .             .             .             .             .             .             .
m_nrmv1_v2v1             0     .44693811             0     .48716571             0     .48158516             0
m_nrmv1_v3v1      .0061528     .65000772             0     .71835595             0     .70352968             0
m_nrmv1_v3v2             0     .48541167             0     .54997501             0     .53879308             0
       blank             .             .             .             .             .             .             .
  m_nrmv1_v1    -.27794925     .01186767    -.30637746     .01992575    -.29707045     .01806331    -.30211863
  m_nrmv1_v2     -.1133532     .16898886    -.13367362     .20026256    -.13163715     .19732355    -.13117232
  m_nrmv1_v3     .01802048     .37205847     .01097183     .43114508     .01268196     .42173916      .0123821
pcr_nrmv1_v1             0             .             .             .             .             .             .
pcr_nrmv1_v2             0             .             .             .             .             .             .
pcr_nrmv1_v3             0             .             .             .             .             .             .
       blank             .             .             .             .             .             .             .
m_nrmv1nb_~1             0     .43138922             0     .47161683             0     .46603627             0
m_nrmv1nb_~1     .00984413     .64508338       .001329     .71343162     .00276595     .69860534       .001329
m_nrmv1nb_~2             0     .48311798             0     .54768131             0     .53649939             0
       blank             .             .             .             .             .             .             .
m_nrmv1nb_v1    -.27216412     .01023041    -.30059233     .01828848    -.29128532     .01642605     -.2963335
m_nrmv1nb_v2    -.11019871     .15922511    -.13051913     .19049881    -.12848265      .1875598    -.12801783
m_nrmv1nb_v3     .02007454     .37291927     .01302589     .43200588     .01473603     .42259996     .01443616
pcr_nrmv1n~1             0             .             .             .             .             .             .
pcr_nrmv1n~2             0             .             .             .             .             .             .
pcr_nrmv1n~3             0             .             .             .             .             .             .
       blank             .             .             .             .             .             .             .

               90_bci_high   90_imci_low  90_imci_high         blank
  m_nex_v2v1     .00554938     .00164276     .00554938             .
  m_nex_v3v1     .04013479     .01738462     .04013479             .
  m_nex_v3v2     .03668857     .01357192     .03668857             .
       blank             .             .             .             .
    m_nex_v1     .00517932     .00391004     .00517932             .
    m_nex_v2     .00994107       .006335     .00994107             .
    m_nex_v3     .04464614     .02218763     .04464614             .
  pcr_nex_v1             .             .             .             .
  pcr_nex_v2             .             .             .             .
  pcr_nex_v3             .             .             .             .
       blank             .             .             .             .
  nobs_nomiv             .             .             .             .
   nobs_miv1             .             .             .             .
       blank             .             .             .             .
    m_n_v2v1     .88561893    -.89240268     .88246286             .
    m_n_v3v1     .89244727    -.88343028     .88987347             .
    m_n_v3v2     .86817546    -.84878118     .86557532             .
       blank             .             .             .             .
      m_n_v1      .5730503    -.35392206     .56969977             .
      m_n_v2     .54099523    -.33093886     .53637952             .
      m_n_v3     .54810656    -.32317441     .54430425             .
    pcr_n_v1             .             .             .             .
    pcr_n_v2             .             .             .             .
    pcr_n_v3             .             .             .             .
       blank             .             .             .             .
   m_nr_v2v1     .61006519             0     .60878874             .
   m_nr_v3v1     .89244727             0     .88987347             .
   m_nr_v3v2     .70848559             0     .70738952             .
       blank             .             .             .             .
     m_nr_v1     .01989143    -.35392206     .01886475             .
     m_nr_v2     .27122488    -.17812989      .2680764             .
     m_nr_v3     .54810656      .0124913     .54430425             .
   pcr_nr_v1             .             .             .             .
   pcr_nr_v2             .             .             .             .
   pcr_nr_v3             .             .             .             .
       blank             .             .             .             .
m_nrmv1_v2v1     .48405469             0     .47395606             .
m_nrmv1_v3v1     .70802738     .00064749     .69174443             .
m_nrmv1_v3v2     .54204963             0     .52703878             .
       blank             .             .             .             .
  m_nrmv1_v1     .01802778    -.29286006     .01669906             .
  m_nrmv1_v2     .19604025    -.12761112      .1910844             .
  m_nrmv1_v3     .42237869     .01385748     .41079974             .
pcr_nrmv1_v1             .             .             .             .
pcr_nrmv1_v2             .             .             .             .
pcr_nrmv1_v3             .             .             .             .
       blank             .             .             .             .
m_nrmv1nb_~1      .4685058             0     .45840718             .
m_nrmv1nb_~1     .70310304     .00432453     .68682009             .
m_nrmv1nb_~2     .53975593             0     .52474509             .
       blank             .             .             .             .
m_nrmv1nb_v1     .01639051    -.28707493      .0150618             .
m_nrmv1nb_v2     .18627649    -.12445663     .18132065             .
m_nrmv1nb_v3     .42323948     .01591154     .41166054             .
pcr_nrmv1n~1             .             .             .             .
pcr_nrmv1n~2             .             .             .             .
pcr_nrmv1n~3             .             .             .             .
       blank             .             .             .             .

. mat2txt, mat(A) saving("${projf}/Results/Bounds/Res_PI_dist_${dist}_b${bsn}_test.txt") replace format(%15.6f) ///      
>   title("Partial identification results using distribution ${dist} and MIV1 ${minstr1}, finished at $S_TIME on $S_DATE") 
>      

. mat drop _all

. 
. 
. *Time stamp
. noisily display "Program started at: ${sttime} of ${stdate}"      /* beginning time stamp */
Program started at: 12:38:58 of 14 Dec 2018

. noisily display "Program finished at: $S_TIME of $S_DATE"       /*ending time stamp*/       
Program finished at: 12:41:52 of 14 Dec 2018

. 
. cap log close
